﻿using DevExpress.Mvvm;
using DevExpress.Xpf.Core;
using HandyControl.Controls;
using Newtonsoft.Json;
using PlaceOrder_Client.Events;
using PlaceOrder_Client.ViewModels;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.Services.Instances;
using PlaceOrder_Shared.Models.Chat;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PlaceOrder_Client.Views
{
    /// <summary>
    /// ChatMessageView.xaml 的交互逻辑
    /// </summary>
    public partial class ChatMessageView : System.Windows.Controls.UserControl
    {
        public ChatMessageView()
        {
            InitializeComponent();

            var items = LoginViewModel.config?.BrokerName;
            if(items!= null)
            {
                foreach (var item in items)
                {
                    checkComboBox.SelectedItems.Add(item);
                }
            }

            Messenger.Default.Register<string>(this, "AddNewBroker",AddNewBroker);

            if (OrderListViewModel.occupyBrokerInfos != null && OrderListViewModel.occupyBrokerInfos.Count > 0)
            {
                var occupyBrokers = OrderListViewModel.occupyBrokerInfos.Where(o => o.Occupy == true && o.Occupyier == UserHelper.UserID).ToList();
                foreach (var item in occupyBrokers)
                {
                    string brokerName = OrderListViewModel.ChatReceivers.Where(o => o.Brokerid == item.BrokerId).FirstOrDefault().Company;
                    Messenger.Default.Send(brokerName, "AddNewBroker");

                }
            }
        }

        private void AddNewBroker(string brokerName)
        {
            checkComboBox.Dispatcher.Invoke(() =>
            {
                if (checkComboBox.SelectedItems.Contains(brokerName))
                {
                    return;
                }
                else
                {
                    checkComboBox.SelectedItems.Add(brokerName);
                }
            });
        }

        private void CheckComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(e.AddedItems.Count > 0)
            {
                List<string> addedItemsList = e.AddedItems.Cast<string>().ToList();
                ChatCheckBoxEventService.OnCheckComboBoxSelectionChanged(addedItemsList, null);
            }


            if (e.RemovedItems.Count > 0)
            {
                List<string> removedItemsList = e.RemovedItems.Cast<string>().ToList();
                ChatCheckBoxEventService.OnCheckComboBoxSelectionChanged(null, removedItemsList);
            }
        }

        /// <summary>
        /// 保存聊天窗口配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
               

                // 获取 checkComboBox 的选中项并保存到 LoginViewModel.config.BrokerName
                LoginViewModel.config.BrokerName = checkComboBox.SelectedItems.Cast<string>().ToList();
                // 将配置对象序列化为 JSON 字符串
                string json = JsonConvert.SerializeObject(LoginViewModel.config, Formatting.Indented);
                // 将 JSON 字符串保存到文件
                string filePath = MainViewModel.ConfigFilePath; // 你可以指定保存的文件路径
                File.WriteAllText(filePath, json);

                Growl.SuccessGlobal($"保存{checkComboBox.SelectedItems.Count}个聊天窗口成功！");
            }catch(Exception ex)
            {
                AppLog.WriteError("保存聊天配置异常！" + ex.StackTrace + ex.Message);
            }
         
        }
    }
}
